package com.wy.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.sun.org.apache.xpath.internal.operations.Or;
import com.wy.common.BaseContext;
import com.wy.common.R;
import com.wy.controller.dto.ManagePieDTO;
import com.wy.entity.Diagnosis;
import com.wy.entity.Order;
import com.wy.entity.Reserve;
import com.wy.entity.User;
import com.wy.service.IDiagnosisService;
import com.wy.service.IOrderService;
import com.wy.service.IReserveService;
import com.wy.service.IUserService;
import com.wy.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 作者：王昭贵
 * 日期：2023/5/19
 */
@Slf4j
@RestController
@RequestMapping("/manage")
public class ManageController {
    @Resource
    private IReserveService reserveService;
    @Resource
    private IOrderService orderService;
    @Resource
    private IDiagnosisService diagnosisService;
    @Resource
    private IUserService userService;

    /****
     * 后台主页统计项--获取明天的已预约总人数
     * @return
     */
    @GetMapping("/getTodayReserveCount")
    private R getTodayReserveCount() {

        LocalDate currentDate = LocalDate.now();
        // 获取明天日期
        LocalDate tomorrowDate = currentDate.plusDays(1);
        // 定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 将明天日期转换为字符串
        String tomorrowDateString = tomorrowDate.format(formatter);
        List<Reserve> list = reserveService.list();
        List<Reserve> list1 = list.stream().filter(item -> DateUtils.formatDate
                (item.getReserveTime(), "yyyy-MM-dd").equals(tomorrowDateString)).collect(Collectors.toList());
        return R.success(list1.size());
    }

    @GetMapping("/getWeekDrugOrder")
    private R getWeekDrugOrder() {
        List<Order> list = orderService.list();
        List<Order> collect = list.stream().filter(item -> item.getDiagnosisId() == null && getDayIsAterWeek(item.getCreateTime())).collect(Collectors.toList());
        return R.success(collect.size());
    }

    /****
     * 后台主页统计项--获取今日已就诊总人数
     * @return
     */
    @GetMapping("/getTodayDiagnosisCount")
    private R getTodayDiagnosisCount() {
        List<Diagnosis> list = diagnosisService.list();
        LocalDate currentDate = LocalDate.now();
        List<Diagnosis> collect = list.stream().filter(item -> item.getCreateTime().toLocalDate().
                compareTo(currentDate) == 0).collect(Collectors.toList());
        return R.success(collect.size());
    }

    /****
     * 获取饼图 用户统计数据
     * @return
     */
    @GetMapping("/getUserRoleDistributionMap")
    private R getUserRoleDistributionMap() {
        List<User> list = userService.list();
        long userCount = list.stream().filter(item -> item.getRole().equals("ROLE_USER")).count();
        long doctorCount = list.stream().filter(item -> item.getRole().equals("ROLE_DOCTOR")).count();
        long petAuxiliaryCheckCount = list.stream().filter(item -> item.getRole().equals("ROLE_AULIXIARYCHECK")).count();
        long adminCount = list.stream().filter(item -> item.getRole().equals("ROLE_ADMIN")).count();
        List<ManagePieDTO> list1 = new ArrayList<>();
        ManagePieDTO managePieDTO = new ManagePieDTO();
        managePieDTO.setValue(StrUtil.toString(userCount));
        managePieDTO.setName("普通用户");
        list1.add(managePieDTO);
        managePieDTO = new ManagePieDTO();
        managePieDTO.setValue(StrUtil.toString(doctorCount));
        managePieDTO.setName("医生");
        list1.add(managePieDTO);
        managePieDTO = new ManagePieDTO();
        managePieDTO.setValue(StrUtil.toString(petAuxiliaryCheckCount));
        managePieDTO.setName("辅助检查结果发布人员");
        list1.add(managePieDTO);
        managePieDTO = new ManagePieDTO();
        managePieDTO.setValue(StrUtil.toString(adminCount));
        managePieDTO.setName("管理员");
        list1.add(managePieDTO);
        return R.success(list1);
    }

    /****
     * 判断日期是否是上周内
     * @param date
     * @return
     */
    private boolean getDayIsAterWeek(LocalDateTime date) {
        LocalDate currentDate = LocalDate.now();
        LocalDate lastWeekStart = currentDate.with(java.time.DayOfWeek.MONDAY).minusDays(1);
        LocalDate lastWeekEnd = currentDate.plusDays(1);
        LocalDate targetDate = LocalDate.of(date.getYear(), date.getMonthValue(), date.getDayOfMonth()); // 要判断的目标日期
        if (targetDate.isAfter(lastWeekStart) && targetDate.isBefore(lastWeekEnd)) {
            return true;
        } else {
            return false;
        }
    }


}
